<template>
  <div>
    <web-view :src="url" @message="getMessage"></web-view>
  </div>
</template>

<script>
export default {
  data() {
    return {
      url: '',
      title: '万户健康',
      showWebUrl: false
    };
  },
  onLoad(options) {
    if (options) {
      if (options.url) {
        uni.showLoading({
          title: '加载中',
          mask: true
        });
        let url =
          `https://www.mfish.xyz/${options.externalLinkDomain}/` +
          decodeURIComponent(options.url).split('/').slice(3).join('/');
        // 安卓平台不能使用webview打开pdf,ios可以，所有ios使用web-view打开pdf
        // console.log('url:', url);
        this.url = url;
        let systemInfo = uni.getSystemInfoSync();
        if (url.indexOf('pdf') > -1 && systemInfo.platform !== 'ios') {
          // 安卓先下载文件，再打开
          wx.downloadFile({
            url: url,
            success: (res) => {
              console.log('downloadFile res:', res);
              uni.hideLoading();
              let filePath = res.tempFilePath;
              uni.showLoading({
                title: '正在打开',
                mask: true
              });
              wx.openDocument({
                filePath: filePath,
                fileType: 'pdf',
                success: (openRes) => {
                  uni.hideLoading();
                },
                fail: (err) => {
                  console.log('err:', err);
                  uni.showModal({
                    title: '提示',
                    content: 'pdf文件打开失败',
                    complete: () => {
                      uni.navigateBack();
                    }
                  });
                }
              });
            }
          });
        }
      }
      if (options.title) {
        this.title = decodeURIComponent(options.title) || '万户健康';
      }
    }
    uni.setNavigationBarTitle({
      title: this.title
    });
    if (!this.url) {
      uni.showModal({
        title: '提示',
        content: '链接错误，无法打开',
        success: function () {
          uni.navigateBack();
        }
      });
    }
  },
  methods: {
    getMessage(event) {
      uni.showModal({
        content: JSON.stringify(event.detail),
        showCancel: false
      });
    }
  }
};
</script>

<style></style>
